package com.lanou3g.hw0413;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by dllo on 18/4/9.
 */
public class Main {

    public static void main(String[] args) {

        User user1 = new User();
        user1.setPwd("a");
        user1.setUsername("b");

        User user2 = new User();
        user2.setPwd("a");
        user2.setUsername("b");

        User user3 = user1;   //user3 与user2 不一样(在内存中画的圈不一样)
        user3.setPwd("sdsdd");  //改变的是user1 的内容,user2 没变化


        Scanner scanner = new Scanner(System.in);

        Menu menu = new Menu();
        Util util = new Util();  //用来登录/注册的
        User user = null;  //用来存用户信息的


        List<User> users = new ArrayList<>();  //用来存储多个用户信息的集合
        users = getFileInfo();
        while (true) {
            menu.showMenu();
            int choice = scanner.nextInt();
            switch (choice) {
                case 1://注册
                    user = util.register();
                    users.add(user);
                    break;
                case 2://登录
                    boolean loginSuccess = util.login(users);
                    if (loginSuccess) {
                        //登录成功
                        System.out.println("欢迎登录");
                        return; //主方法走完了,程序结束
                    } else {
                        //登录失败
                        // 返回主菜单
                        System.out.println("用户名/密码错误");
                        System.out.println("继续循环,可以省略else");
                    }
                    break;
                default:
                    System.out.println("只能输入1或2");
            }
        }
    }


    public static List<User> getFileInfo() {


        List<User> userArrayList = new ArrayList<>();
        File file = new File("information.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buff = new byte[1024];
            int num = 0;
            StringBuilder sb = new StringBuilder();
            while ((num = fis.read(buff)) != -1) {
                // 从字节数组生成一个字符串
                String data = new String(buff, 0, num);
                sb.append(data);
            }
            // 将sb 装成String
            String result = sb.toString();
            userArrayList = getUsers(result);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis == null){
                    fis.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return userArrayList;
    }

    public static List<User> getUsers(String infoString) {
        List<User> userArrayList = new ArrayList<>();
        String[] nameAndPwdString = infoString.split(";");

        for (String s : nameAndPwdString) {
            String[] userString = s.split(":");

            if (userString.length == 2) {
                User user = new User();
                user.setUsername(userString[0].trim());
                user.setPwd(userString[1].trim());
                userArrayList.add(user);
            }

        }

        return userArrayList;
    }


}
